package org.anti996.bankms.controller;

import org.anti996.bankms.dao.UserDao;
import org.anti996.bankms.model.Card;
import org.anti996.bankms.model.TRecord;
import org.anti996.bankms.util.IOUtil;
import org.anti996.bankms.util.MessageUtil;
import org.anti996.bankms.view.UserCardView;

import java.util.List;

public class UserCardController extends ControllerBase {
    private final List<Card> cards_;

    public UserCardController(List<Card> cards) {
        super(new UserCardView(cards));
        cards_ = cards;
    }

    @Override
    public void deal() {
        super.deal();
        int choose = IOUtil.parseOrder();
        if (choose < 1 || cards_.size() < choose) {
            wrongInputAndRetry();
            return;
        }
        Card card = cards_.get(choose - 1);
        MessageUtil.infoln("当前卡：");
        MessageUtil.infoln(Card.describe(card));
        MessageUtil.infoln("转账历史记录：");
        var recList = UserDao.getRecords(card);
        if (recList.isEmpty()) {
            MessageUtil.warning("列表为空");
        } else {
            String[] recs = new String[recList.size()];
            for (int i = 0; i < recList.size(); i++) {
                TRecord rec = recList.get(i);
                recs[i] = TRecord.describe(card, rec);
            }
            MessageUtil.itemList(recs);
        }
        Navigator.pop();
        waitForKeyPress();
    }
}
